﻿using AutoMapper;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.VModel;
using System;
using System.Collections.Generic;
using System.Linq;
using IOA.MES.DataAccess.Enum;

namespace IOA.MES.Business
{
    /// <summary>
    /// IV本地数据格式配置（错误代码：122001）
    /// </summary>
    public class IVLocalDataMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// 编辑IVSQL
        /// </summary>
        public void Edit(VIVLocalData model)
        {
            var exists = mesDb.IVLocalDatas.Where(_ => _.MachineId == model.MachineId && _.Valid).ToList();
            foreach (var item in exists)
            {
                item.Valid = false;
                item.ModifyBy = model.ModifyBy;
                item.ModifyTime = DateTime.Now;
            }

            var entity = new IVLocalData
            {
                MachineId = model.MachineId,
                Sql = model.Sql,
                CreateBy = model.ModifyBy,
                CreateTime = DateTime.Now
            };
            mesDb.IVLocalDatas.Add(entity);
            mesDb.SaveChanges();
        }

        /// <summary>
        /// 查询
        /// </summary>
        public VIVLocalData GetIVSql(int machineId)
        {
            var entity = mesDb.IVLocalDatas.FirstOrDefault(_ => _.MachineId == machineId && _.Valid);
            return Mapper.Map<VIVLocalData>(entity);
        }

        /// <summary>
        /// 查询IV配置
        /// </summary>
        /// <param name="workshop">车间代码</param>
        public List<VIVLocalInfo> Query(string workshop = "")
        {
            var query = from machine in mesDb.Machines
                        join iv in mesDb.IVLocalDatas.Where(_ => _.Valid) on machine.PkId equals iv.MachineId into ivTmp
                        from iv1 in ivTmp.DefaultIfEmpty()
                        join line in mesDb.AssemblyLines.Where(_ => _.Valid) on machine.LineId equals line.PkId into tmp
                        from line1 in tmp.DefaultIfEmpty()
                        where machine.Valid && machine.MachineType == MachineType.IV
                        select new VIVLocalInfo
                        {
                            MachineId = machine.PkId,
                            WorkshopCode = machine.WorkshopCode,
                            WorkshopName = machine.WorkshopName,
                            LineName = line1.LineName,
                            MachineName = machine.MachineName,
                            MachineModel = machine.MachineModel,
                            Manufacturers = machine.Manufacturers,
                            Sql = iv1.Sql
                        };
            if (!string.IsNullOrWhiteSpace(workshop))
            {
                query = query.Where(_ => _.WorkshopCode.Contains(workshop) || _.WorkshopName.Contains(workshop));
            }

            var entities = query.OrderBy(_ => _.WorkshopCode).ThenBy(_ => _.LineName).ToList();

            return entities;
        }

        /// <summary>
        /// 查询IV配置
        /// </summary>
        /// <param name="machineIds">设备ID列表</param>
        public List<VIVLocalData> Query(List<int> machineIds)
        {
            var entities = mesDb.IVLocalDatas.Where(_ => machineIds.Contains(_.MachineId) && _.Valid).ToList();
            return Mapper.Map<List<VIVLocalData>>(entities);
        }
    }
}

